package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.yy.common.base.BaseAdapter;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonutils.ToastUitl;
import com.yy.jhl.R;
import com.yy.jhl.bean.ProductInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class ProductAdapter extends BaseAdapter {
    private List<ProductInfo> modelList;
    private List<ProductInfo> selectList;
    private boolean isEdit;

    public void setSelectList(List<ProductInfo> selectList) {
        this.selectList = selectList;
    }

    public ProductAdapter(Context context, boolean isEdit) {
        super(context);
        this.isEdit = isEdit;
        modelList = new ArrayList<>();
    }

    public List<ProductInfo> getModelList() {
        return modelList;
    }
    public boolean isHaveData(){
        int size = getCount();
        for (int i=0;i<size;i++){
            if (modelList.get(i).getRealQuantity() != 0){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取输入的产品信息和总价格
     * @return
     */
    public  Map<String ,String> getAllPrice(){
        Map<String ,String> map = new HashMap<>();
        double price = 0d;
        JSONArray items = new JSONArray();
        int size = getCount();
        for (int i=0;i<size;i++){
            if (modelList.get(i).getRealQuantity() != 0){
                price+=modelList.get(i).getRealQuantity()*Double.parseDouble(modelList.get(i).getUnit_price());
                JSONObject object = new JSONObject();
                try {
                    object.put("order_item_id",modelList.get(i).getOrder_item_id());
                    object.put("quantity",modelList.get(i).getRealQuantity());
                    object.put("batch_order_item_id",modelList.get(i).getBatch_order_item_id());
                    items.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("price",StringUtil.getSingleDouble(price));
        map.put("items",items.toString());
        return map;
    }

    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<ProductInfo> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    public void addAllData(List<ProductInfo> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_product,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        final ProductInfo itemModel = modelList.get(i);
        storeHolder.a_text.setText(itemModel.getItem_name());
        storeHolder.b_text.setText(itemModel.getModel());
        if (isEdit){
            storeHolder.c_text.setVisibility(View.VISIBLE);
            storeHolder.real_num_text.setVisibility(View.GONE);
            if (selectList != null && selectList.contains(itemModel)){
                int index = selectList.indexOf(itemModel);
                if (selectList.get(index).getRealQuantity() != 0 ){
                    storeHolder.c_text.setText(selectList.get(index).getRealQuantity()+"");
                }
            }
        }else {
            storeHolder.c_text.setVisibility(View.GONE);
            storeHolder.real_num_text.setVisibility(View.VISIBLE);
            storeHolder.real_num_text.setText(itemModel.getRealQuantity()+"");
            String allPrice = StringUtil.getSingleDouble(itemModel.getRealQuantity() * Double.parseDouble(itemModel.getUnit_price()));
            storeHolder.f_text.setText(allPrice);
        }
        storeHolder.c_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = editable.toString();
                String allPrice = "";

                if (!StringUtil.isEmpty(content)){
                    try {
                        double realNum = Double.parseDouble(editable.toString());
                        if (realNum > Double.parseDouble(itemModel.getQuantity())){
                            ToastUitl.showShort("输入数量不能超过总数量");
                            storeHolder.c_text.setText("");
                            storeHolder.f_text.setText("0.00");
                        }else {
                            allPrice = StringUtil.getSingleDouble(realNum*Double.parseDouble(itemModel.getUnit_price()));
                            storeHolder.f_text.setText(allPrice);
                            itemModel.setRealQuantity(realNum);
                            modelList.set(i,itemModel);
//                            notifyDataSetChanged();
                        }

                    }catch (Exception e){

                    }
                }

            }
        });
        storeHolder.d_text.setText(itemModel.getUnit());
        storeHolder.e_text.setText(itemModel.getUnit_price());

        return view;
    }
    /**
     *
     */
    class Holder{
        private TextView a_text;
        private TextView b_text;
        private EditText c_text;
        private TextView d_text;
        private TextView e_text;
        private TextView f_text;
        private TextView real_num_text;
        void initView(View view) {
            a_text = (TextView) view.findViewById(R.id.a_text);
            b_text = (TextView) view.findViewById(R.id.b_text);
            c_text = (EditText) view.findViewById(R.id.c_text);
            d_text = (TextView) view.findViewById(R.id.d_text);
            e_text = (TextView) view.findViewById(R.id.e_text);
            f_text = (TextView) view.findViewById(R.id.f_text);
            real_num_text = (TextView) view.findViewById(R.id.real_num_text);
        }

    }
}
